package com.queclink.ycm.entity.data;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.queclink.ycm.common.utils.HexUtils;

import java.util.List;
import java.util.Map;

/**
 * @author jim.li
 * @email jim.li@queclink.com
 * @date 2021-12-24 17:30:00
 */
public class Data89 extends Data {

    private String Number;
    private String GSV;

    public Data89() {
    }

    @JSONField(
            name = "Number"
    )
    public String getNumber(JSONObject jsonObject) {
        Integer number = jsonObject.getInteger("Number");
        return HexUtils.intToHex(number, 2);
    }

    @JSONField(
            name = "GSV"
    )
    public String getGSV(JSONObject jsonObject) {
        StringBuffer sb = new StringBuffer();
        List<Map<String, Integer>> dtCs = (List<Map<String, Integer>>) jsonObject.get("GSV");
        for (Map<String, Integer> dtC : dtCs) {
            Integer gnssid = Integer.valueOf(dtC.get("GNSSID").toString());
            String gnssidStr = HexUtils.intToHex(gnssid, 2);
            sb.append(gnssidStr);
            Integer svid = Integer.valueOf(dtC.get("SVID").toString());
            String svidStr = HexUtils.intToHex(svid, 2);
            sb.append(svidStr);
            Integer signalStrength = Integer.valueOf(dtC.get("SignalStrength").toString());
            String signalStrengthStr = HexUtils.intToHex(signalStrength, 2);
            sb.append(signalStrengthStr);
            Integer elevation = Integer.valueOf(dtC.get("Elevation").toString());
            String elevationStr = HexUtils.intToHex(elevation, 2);
            sb.append(elevationStr);
            Integer azimuth = Integer.valueOf(dtC.get("Azimuth").toString());
            String azimuthStr = HexUtils.intToHex(azimuth, 4);
            sb.append(azimuthStr);
        }
        return sb.toString();
    }

    public String getDataID() {
        // data89
        return HexUtils.intToHex(89, 2);
    }


    @Override
    public String combine(JSONObject jsonObject) throws Exception {
        StringBuffer sb = new StringBuffer();

        sb.append(this.getNumber(jsonObject));
        sb.append(this.getGSV(jsonObject));

        int len = sb.length();
        len = len / 2;
        if (len > 127) {
            sb.insert(0, HexUtils.intToHex(len, 4));
        } else {
            sb.insert(0, HexUtils.intToHex(len, 2));
        }
        sb.insert(0, this.getDataID());
        return sb.toString();
    }

}
